package com.example.anju.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.anju.entity.House;
import com.example.anju.entity.Information;
import com.example.anju.entity.Orders;
import com.example.anju.entity.Result;
import com.example.anju.mapper.OrdersMapper;

@Service
public class OrdersService {
	@Autowired
	private OrdersMapper ordersMapper;

	// 通过orders_id查询订单
	public Result findOrdersByOrdersId(Orders orders) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);
		try {
			List<Orders> ordersByOrdersId = ordersMapper.findOrdersByOrdersId(orders.getOrders_id());
			if (ordersByOrdersId.isEmpty()) {
				result.setMessage("无订单");
			} else {
				result.setMessage("查找订单成功");
				result.setSuccess(true);
				result.setDatas(ordersByOrdersId);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

	// 通过orders_landlord_id查询订单
	public Result findOrdersByLandlordId(Orders orders) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);
		try {
			List<Orders> ordersByLandlordId = ordersMapper.findOrdersByLandlordId(orders.getOrders_landlord_id());
			if (ordersByLandlordId.isEmpty()) {
				result.setMessage("无订单");
			} else {
				result.setMessage("查找订单成功");
				result.setSuccess(true);
				result.setDatas(ordersByLandlordId);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

	// 通过orders_tenant_id查询订单
	public Result findOrdersByTenantId(Orders orders) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);
		try {
			List<Orders> ordersByTenantId = ordersMapper.findOrdersByTenantId(orders.getOrders_tenant_id());
			if (ordersByTenantId.isEmpty()) {
				result.setMessage("无订单");
			} else {
				result.setMessage("查找订单成功");
				result.setSuccess(true);
				result.setDatas(ordersByTenantId);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

	// 通过orders_landlord_id和check_orders查询订单
	public Result findOrdersByLandlordIdAndCheck(Orders orders) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);
		try {
			List<Orders> ordersByLandlordIdAndCheck = ordersMapper.findOrdersByLandlordIdAndCheck(orders);
			if (ordersByLandlordIdAndCheck.isEmpty()) {
				result.setMessage("无订单");
			} else {
				result.setMessage("查找订单成功");
				result.setSuccess(true);
				result.setDatas(ordersByLandlordIdAndCheck);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

	// 通过orders_tenant_id和check_orders查询订单
	public Result findOrdersByTenantIdAndCheck(Orders orders) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);
		try {
			List<Orders> ordersByTenantIdAndCheck = ordersMapper.findOrdersByTenantIdAndCheck(orders);
			if (ordersByTenantIdAndCheck.isEmpty()) {
				result.setMessage("无订单");
			} else {
				result.setMessage("查找订单成功");
				result.setSuccess(true);
				result.setDatas(ordersByTenantIdAndCheck);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

	// 通过orders_tenant_id和orders_house_id查询订单
	public Result findOrdersByTenantIdAndHouseId(Orders orders) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);
		try {
			List<Orders> ordersByTenantIdAndHouseId = ordersMapper.findOrdersByTenantIdAndHouseId(orders);
			if (!ordersByTenantIdAndHouseId.isEmpty()) {
				result.setMessage("已对该房屋下过订单");
			} else {
				result.setMessage("未对该房屋下过订单");
				result.setSuccess(true);
				result.setDatas(ordersByTenantIdAndHouseId);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

	// 生成订单
	public Result publishOrders(Orders orders) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);
		try {
			List<Orders> ordersByTenantIdAndHouseId = ordersMapper.findOrdersByTenantIdAndHouseId(orders);
			List<Orders> ordersByOrdersHouseAndHouseId = ordersMapper.findOrdersByOrdersHouseAndHouseId(orders.getOrders_house_id());
			if (!ordersByTenantIdAndHouseId.isEmpty()) {
				result.setMessage("已对该房屋下过订单");
			} else if (!ordersByOrdersHouseAndHouseId.isEmpty()) {
				result.setMessage("该房屋已被房东确认");
			} else {
				ordersMapper.publishOrders(orders);
				result.setMessage("生成订单成功");
				result.setSuccess(true);
				ordersByTenantIdAndHouseId = ordersMapper.findOrdersByTenantIdAndHouseId(orders);
				result.setDatas(ordersByTenantIdAndHouseId);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

	// 通过orders_id修改订单状态
	public Result postOrdersByOrdersIdAndCheck(Orders orders) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);
		try {
			List<Orders> ordersByOrdersIdAndCheck = ordersMapper.findOrdersByOrdersIdAndCheck(orders);
			if (!ordersByOrdersIdAndCheck.isEmpty()) {
				result.setMessage("订单已被确认");
			} else {
				ordersMapper.postOrdersByOrdersId(orders);
				result.setMessage("订单确认成功");
				result.setSuccess(true);
				ordersByOrdersIdAndCheck = ordersMapper.findOrdersByOrdersIdAndCheck(orders);
				result.setDatas(ordersByOrdersIdAndCheck);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

}
